//
//  BiddingControllerViewController.swift
//  777Proj
//
//  Created by 陈宇 on 2017/5/15.
//  Copyright © 2017年 🐜. All rights reserved.
//

import UIKit
import Haneke
import PullToRefresh

enum BiddingType {
    case all
    case bright
    case dark
    case history
    
    func apiIndex() -> String {
        switch self {
        case .all:
            return "0"
        case .bright:
            return "1"
        case .dark:
            return "2"
        case .history:
            return "3"
        }
    }
}

class BiddingController: BaseTableViewController<BaseBean> {
    
    var type: BiddingType = .all
    
    override var tableStyle: UITableViewStyle {
        return .grouped
    }
    
    override var topRefreshable: Bool {
        return true
    }
    
    convenience init(_ type: BiddingType) {
        self.init()
        self.type = type
    }
    
    override func configureLeftItem() {
        addLeftBarButtonWithImage(#imageLiteral(resourceName: "menu"))
    }
    
    override func configureUI() {
        super.configureUI()
        
        tableView.register(nib: UINib(nibName: "BiddingCell", bundle: nil), withCellClass: BiddingCell.self)
        tableView.separatorStyle = .none
        tableView.rowHeight = 100
        tableView.hideEmpty()
        
        dataSource.append([BaseBean()])
        dataSource.append([BaseBean()])
        dataSource.append([BaseBean()])
        dataSource.append([BaseBean()])
        dataSource.append([BaseBean()])
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: BiddingCell.self)!
        cell.coverView?.hnk_setImageFromURL(APP.APP_PLACEHOLDER_URL, format: Format(name: "cell"))
        
        if self.type == .bright {
            cell.stateLabel.state = .bright
        } else if self.type == .dark {
            cell.stateLabel.state = .dark
        } else {
            let index = arc4random() % 4
            if index == 0 {
                cell.stateLabel.state = .dark
            } else if index == 1 {
                cell.stateLabel.state = .bright
            } else if index == 2 {
                cell.stateLabel.state = .sale
            } else if index == 3 {
                cell.stateLabel.state = .fail
            }
        }
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return section == 0 ? 5 : 0.01
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 5
    }
    
    override func configureEvent() {
        refreshData()
    }
    
    func refreshData(_ position: Position = .top) {
        api.request(.BidList(state: self.type)).unWrapper(error: { code in
            self.tableView.endRefreshing(at: position)
        }).mapArray(type: House.self).subscribe(onNext: { (houses) in
            self.dataSource.append(contentsOf: houses.map { [$0] })
            self.tableView.reloadData {
                self.tableView.endRefreshing(at: position)
            }
        }, onError: { _ in
            self.tableView.endRefreshing(at: position)
        }).addDisposableTo(disposeBag)
    }
    
    override func refreshing(_ position: Position) {
        switch position {
        case .top:
            dataSource.removeAll()
            refreshData()
        case .bottom:
            refreshData(.bottom)
        }
    }
    
}
